package com.captainbank.tcl.tclapplication.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by TCL on 2019/2/12.
 * Describe:
 */
public class SharePreferenceManager {
    public static final String Live = "com.captainbank.live";

    static SharedPreferences sp;

    public static void init(Context context, String name) {
        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    public static void clear() {
        if (null != sp) {
            sp.edit().clear().apply();
        }
    }

    /**
     * Preferences:
     *
     */

    //直播推流地址
    private static final String LIVE_RTMPURL = "live_rtmpurl";
    public static void setLiveRtmpurl(String url) {
        if (null != sp) {
            sp.edit().putString(LIVE_RTMPURL, url).apply();
        }
    }

    public static String getLiveRtmpurl() {
        if (null != sp) {
            return sp.getString(LIVE_RTMPURL, "");
        }
        return "";
    }

    private static final String LIVE_VIDEO = "live_video";
    public static void setLiveVideoUrl(String url) {
        if (null != sp) {
            sp.edit().putString(LIVE_VIDEO, url).apply();
        }
    }

    public static String getLiveVideoUrl() {
        if (null != sp) {
            return sp.getString(LIVE_VIDEO, "");
        }
        return "";
    }



    /**
     * Temp:
     */


    /*
     *
     */
    private static <T> void saveList(String key, List<T> list) {
        if (null != sp) {
            if(list!=null){
                Type personType = new TypeToken<List<T>>() {}.getType();
                Gson gson = new Gson();
                String json = gson.toJson(list, personType);
                sp.edit().putString(key, json).apply();
            }else{
                sp.edit().putString(key, "").apply();
            }

        }
    }

}
